package main

import (
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strconv"
)

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func doWork(i int, finalMap map[int]string, page chan int) {
	url := "https://www.ebay.co.uk/sch/i.html?_from=R40&_nkw=bluetooth%20headphones&_sacat=0&_clu=2&_fcid=" + strconv.Itoa(i) + "&_localstpos&_stpos&gbr=1"
	fmt.Printf("开始爬取第%d号\n", i)
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("http.Get Error:", err)
		return
	}
	defer resp.Body.Close()
	buf := make([]byte, 1024*4)
	var result string
	for {
		n, _ := resp.Body.Read(buf)
		//fmt.Println(string(buf[:n]))
		result = result + string(buf[:n])
		if n == 0 {
			break
		}
	}
	re := regexp.MustCompile(` selected='selected' >(?s:(.*?))</option>`)
	mapRes := re.FindAllStringSubmatch(result, 1)
	for _, data := range mapRes {
		finalMap[i] = data[1]
		page<-i
	}

	return
}
func main() {
	var start, end int
	fmt.Println("请输入开始编号")
	fmt.Scan(&start)
	fmt.Println("请输入结束编号")
	fmt.Scan(&end)
	page := make(chan int)
	finalMap := make(map[int]string)
	for i := start; i <= end; i++ {
		go doWork(i, finalMap, page)
	}
	for i := start; i <= end; i++ {
		fmt.Printf("第%d号爬取处理完毕\n", <-page)
	}
	file, err := os.Create("eaby-flic.txt")
	defer file.Close()
	if err != nil {
		fmt.Println("os.Create Err:", err)
		return
	}
	for k, v := range finalMap{
        file.WriteString(strconv.Itoa(k) + ": " + v +"\r\n")
	}
}
